{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-11T06:35:25.113359Z",
     "start_time": "2025-09-11T06:35:23.511907Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day14/file/'"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 排序",
   "id": "7cb85f8a3dad2be7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:47:17.087276Z",
     "start_time": "2025-09-11T06:47:17.074704Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 案例一：按理论分数降序排列\n",
    "df = pd.read_excel(path + '成绩.xlsx',index_col='序号')\n",
    "df.sort_values(by='理论', ascending=False, inplace=True)\n",
    "# print(df)\n",
    "\n",
    "# 2.案例二：多列排序（理论降序，机试升序，品德降序）\n",
    "df.sort_values(by=['理论','机试','品德'], ascending=[False,True,False], inplace=True)\n",
    "# print(df)\n",
    "# 3. 案例三：按索引进行排序\n",
    "df.sort_index( inplace=True)\n",
    "print(df)"
   ],
   "id": "59c98e062a3f0d40",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     姓名  理论  机试  品德\n",
      "序号                 \n",
      "1   韩耀祖  65  77  98\n",
      "2   谭鑫宇  80  99  36\n",
      "3   聂茹凤  70  32  15\n",
      "4   崔龙腾  78  78  44\n",
      "5   刘千琪  75  36  75\n",
      "6   李兆康  85  42  33\n",
      "7   李欣桐  96  45  66\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:56:00.923958Z",
     "start_time": "2025-09-11T06:56:00.916117Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_csv(path + '排序.csv')\n",
    "# print(df)\n",
    "\n",
    "# 按照a列升序排序\n",
    "# df.sort_values(by='a', ascending=True, inplace=True)\n",
    "# print(df)\n",
    "\n",
    "# 按照第一行降序排序\n",
    "df.sort_values(by=0,ascending=False, inplace=True, axis=1)\n",
    "print(df)\n"
   ],
   "id": "7bc9b95e6402bc81",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   c  b  a\n",
      "0  0  5  3\n",
      "1  8  5  2\n",
      "2  7  6  9\n",
      "3  4  8  6\n",
      "4  2  1  7\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理缺失值",
   "id": "35b2ac2bfb0979c8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-11T06:56:54.575999Z",
     "start_time": "2025-09-11T06:56:54.569014Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_csv(path + '排序.csv')\n",
    "# 按照a列升序排序\n",
    "df.sort_values(by='a', ascending=True, inplace=True,na_position='first')\n",
    "print(df)"
   ],
   "id": "a94bb85176b5a024",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     a    b    c\n",
      "1  NaN  5.0  8.0\n",
      "0  3.0  5.0  0.0\n",
      "3  6.0  8.0  NaN\n",
      "4  7.0  1.0  2.0\n",
      "2  9.0  NaN  7.0\n"
     ]
    }
   ],
   "execution_count": 30
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
